﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TAB50SPLIT
    { 

       
        public TAB50SPLIT()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            InitializeComponent();
            if (app.User.Trim() == "ITXV")
            {
                KRS = "B5";
                LFD = "000020";
                POS = "010";
            }
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwAB = dwMaster;
            if (dwAB != null)
            {
                KEY = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
            }
            else
            {
                KEY = "";
            }
        }

        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
            if (MV == null)
                return;
            if (ORDERNUM.Length < 11)
                return;
            KRS = ORDERNUM.Substring(0, 2);
            LFD = ORDERNUM.Substring(2, 6);
            POS = ORDERNUM.Substring(8, 3);
            PM["ORDERNUM"] = ORDERNUM;
            TMV.LOAD(PM);
            vwMaster.View.MoveCurrentToFirst();

        }

        private void btnSplitBatch_Click(object sender, RoutedEventArgs e)
        {
            if (dwRule == null)
            {
                MessageBox.Show("唉，我是谁?");
                return;
            }

            if (SQTY < 0)
            {
                MessageBox.Show("请输入有效的拆分数量");
                return;
            }
            if (POS == "000" || POS.Trim().Length == 0)
            {
                MessageBox.Show("请输入有效的批次，例如011,注意不能和现有的批次重复");
                return;
            }
            if (dwAB == null)
            {
                MessageBox.Show("请选择对应的粗纱生产单");
                return;
            }
            if (dwAB["ABPPOSTYP"].ToString().Trim() != "HM")
            {
                MessageBox.Show("仅对混棉单支持分批");
                return;
            }
            try
            {

                double QTYTOTAL = 0;
                //定位需要处理的生产单              
                //拆分数量
                //更新，并创建 

                //哪个汉字不懂？找到这些生产单对应的AB52，修改生产单号码和行号，进行行号更新，保存
                QTYTOTAL = Convert.ToDouble(dwAB["ABPSOLLMENGE7"].ToString());

                string order = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString();

                double RATIO = Math.Round(SQTY / QTYTOTAL, 5);

                double ABPSOLLMENGE5 = Convert.ToDouble(dwAB["ABPSOLLMENGE5"].ToString());
                double ABPSOLLMENGE7 = Convert.ToDouble(dwAB["ABPSOLLMENGE7"].ToString());
                dwAB["ABPSOLLMENGE5"] = Math.Round(ABPSOLLMENGE5 * (1 - RATIO) / 100) * 100;
                dwAB["ABPSOLLMENGE7"] = Math.Round(ABPSOLLMENGE7 * (1 - RATIO) / 100) * 100;
                TMV.UpdateABPOS(dwAB, 7);
                DataRow dwnAB = TBMain.NewRow();
                dwnAB.ItemArray = dwAB.ItemArray;
                dwnAB["ABPAUFPOS"] = POS;
                dwnAB["ABPSOLLMENGE5"] = Math.Round(ABPSOLLMENGE5 * (RATIO) / 100) * 100;
                dwnAB["ABPSOLLMENGE7"] = Math.Round(ABPSOLLMENGE7 * (RATIO) / 100) * 100;

                dwRule["QTYEND"] = Math.Round(ABPSOLLMENGE5 * (1 - RATIO) * 0.01);
               
                DataRow dwnRule = dwRule.Table.Clone().NewRow();
                dwnRule.ItemArray = dwRule.ItemArray;
                dwnRule["ROWNID"] = INTEXDBC.GetSeq();
                dwnRule["QTYEND"] = Math.Round(ABPSOLLMENGE5 * (RATIO) * 0.01);
                dwnRule["ORDERNUM"] = dwnAB["ABPAUFKRS"].ToString() + dwnAB["ABPAUFLFD"].ToString() + dwnAB["ABPAUFPOS"].ToString() + dwnAB["ABPPOSTYP"].ToString();
             
                TMV.InsertABPOS(dwnAB, app.company);

                PM["ORDERNUM"] = order;
                SResult = VABPR.Instance.LOAD(PM);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwData = VABPR.Instance.TBMain.NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["ARAUFPOS"] = POS;
                    VABPR.Instance.InsertABPR(dwData, app.company);
                }
                PM["ORDERNUM"] = order;
                SResult = VGAAUF.Instance.LOAD(PM);
                double QTYMAT = 0, nQTYMAT = 0;
                foreach (DataRow dwGD in SResult.Tables[0].Rows)
                {
                    VGLLG.Instance.RVZUTEI(dwGD);

                    double GAKG1 = Convert.ToDouble(dwGD["GAKG1"].ToString());
                    double GAKG2 = Convert.ToDouble(dwGD["GAKG2"].ToString());
                    dwGD["GAKG1"] = Math.Round(GAKG1 * (1 - RATIO) / 100) * 100;
                    dwGD["GAKG2"] = Math.Round(GAKG2 * (1 - RATIO) / 100) * 100;
                    VGAAUF.Instance.UpdateGAAUF(dwGD, 0);
                    QTYMAT += Convert.ToDouble(dwGD["GAKG1"] .ToString())*0.01;
                    
                    dwData = VGAAUF.Instance.TBMain.NewRow();
                    dwData.ItemArray = dwGD.ItemArray;
                    dwData["GAAUFPOS"] = POS;
                    dwData["GAKG1"] = Math.Round(GAKG1 * (RATIO) / 100) * 100;
                    dwData["GAKG2"] = Math.Round(GAKG2 * (RATIO) / 100) * 100;
                    VGAAUF.Instance.InsertGAAUF(dwData, app.company);
                    nQTYMAT += Convert.ToDouble(dwData["GAKG1"].ToString()) * 0.01;

                }
                
                dwRule["QTYMAT"] = QTYMAT;
                VICC_PRULES.Instance.UpdateICC_PRULES(dwRule, 1);
             
                dwnRule["QTYMAT"] = nQTYMAT;
                VICC_PRULES.Instance.InsertICC_PRULES(dwnRule, app.company);

                TMV.UpdateABPOS(dwAB, 6);
                PM["ORDERNUM"] = order;
                SResult = TMV.VABF.LOAD(PM);
                DataTable TBF = SResult.Tables[0].Clone();
                foreach (DataRow dwAF in SResult.Tables[0].Rows)
                {
                    double ABFSOLLMG = Convert.ToDouble(dwAF["ABFSOLLMG"].ToString());
                    dwAF["ABFSOLLMG"] = Math.Round(ABFSOLLMG * (1 - RATIO) / 100) * 100;
                    TMV.VABF.UpdateABFOLGE(dwAF, 0);
                    dwData = TBF.NewRow();
                    dwData.ItemArray = dwAF.ItemArray;
                    dwData["ABFAUFPOS"] = POS;
                    dwData["ABFSOLLMG"] = Math.Round(ABFSOLLMG * (RATIO) / 100) * 100;
                    TMV.VABF.InsertABFOLGE(dwData, app.company);
                }
                PM["ORDERNUM"] = order;
                SResult = VABAUFM.Instance.ABLOAD(PM);
                foreach (DataRow dwMU in SResult.Tables[0].Rows)
                {
                    double ABARESMG = Convert.ToDouble(dwMU["ABARESMG"].ToString());
                    double ABASANZSTK = Convert.ToDouble(dwMU["ABASANZSTK"].ToString());
                    double ABASGESGEW = Convert.ToDouble(dwMU["ABASGESGEW"].ToString());

                    dwMU["ABARESMG"] = Math.Round(ABARESMG * (1 - RATIO) / 100) * 100;
                    dwMU["ABASANZSTK"] = Math.Round(ABASANZSTK * (1 - RATIO) / 100) * 100;
                    dwMU["ABASGESGEW"] = Math.Round(ABASGESGEW * (1 - RATIO) / 100) * 100;//预留数量
                    VABAUFM.Instance.UpdateABAUFM(dwMU, app.company);
                    dwData = VABAUFM.Instance.TBMain.NewRow();
                    dwData.ItemArray = dwMU.ItemArray;
                    dwData["ABAAUFPOS"] = POS;
                    dwData["ABARESMG"] = Math.Round(ABARESMG * (RATIO) / 100) * 100;
                    dwData["ABASANZSTK"] = Math.Round(ABASANZSTK * (RATIO) / 100) * 100;
                    dwData["ABASGESGEW"] = Math.Round(ABASGESGEW * (RATIO) / 100) * 100;
                    VABAUFM.Instance.InsertABAUFM(dwData, app.company);
                }
                order = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + POS + dwAB["ABPPOSTYP"].ToString();
                PM["RUNID"] = this.RUNID;
                PM["ORDERNUM"] = order;
                VABPOS.Instance.LOADABP(PM);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                EventDispatcher.Instance.RaiseStatus("异常退出", StatusEventType.StopProgress);
            }
            finally
            {
                SQTY = -1;
                POS = "000";
            }
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            base.QueryCommand(sender, e);
            ORDERNUM = KRS + LFD + POS;
        }

    }
}
